﻿Imports System.ComponentModel
Imports VisualStudioTempFileCleanup.Commands

Namespace ModelView
    Public Class MainWindow
        Implements IDisposable
        Implements INotifyPropertyChanged
        ' Events
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Public Event WorkDone As EventHandler

        ' Methods
        Public Sub New()
            m_BackgroundWorker = New BackgroundWorker() With {.WorkerReportsProgress = True}
        End Sub

        Private Sub OnBackgroundWorkerDoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles m_BackgroundWorker.DoWork
            DirectCast(e.Argument, Collections.ObjectModel.ObservableCollection(Of Interfaces.IProcess)).Where(Function(item) item.IsEnabled = True).ToList.ForEach(Sub(process)
                                                                                                                                                                        m_BackgroundWorker.ReportProgress(0, process.Name)
                                                                                                                                                                        process.Action()
                                                                                                                                                                    End Sub)

            m_BackgroundWorker.ReportProgress(0, "Done")
        End Sub


        Private Sub OnBackgroundWorkerProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles m_BackgroundWorker.ProgressChanged
            StatusMessage = e.UserState.ToString
        End Sub

        Private Sub OnBackgroundWorkerRunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles m_BackgroundWorker.RunWorkerCompleted
            Close()
        End Sub

        Private Sub OnPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            RaiseEvent PropertyChanged(sender, e)
        End Sub

        Public Sub Start()
            m_BackgroundWorker.RunWorkerAsync(Actions)
        End Sub
        Private WithEvents m_BackgroundWorker As BackgroundWorker

        <Description("Action List"), DisplayName("Actions")>
        Public ReadOnly Property Actions As Collections.ObjectModel.ObservableCollection(Of Interfaces.IProcess)
            Get
                If m_Actions Is Nothing Then
                    m_Actions = ObjectsMethods.ObjectFactory.CreateAListOfInstancesOfAllClassesInTheCurrentAppDomainThatImplementsThisInterface(Of Interfaces.IProcess)()
                    ' Search the all current loaded assemblies for any class that implements IProcess and add it to the list
                End If
                Return m_Actions
            End Get
        End Property
        Private m_Actions As Collections.ObjectModel.ObservableCollection(Of Interfaces.IProcess)

        <Description("Delete Visual Studio Temp Files"), DisplayName("Delete Visual Studio Temp Files")>
        Public ReadOnly Property StartCommand As ICommand
            Get
                If m_StartCommand Is Nothing Then
                    m_StartCommand = New ActionCommand(Sub() Start(), Function() Not m_BackgroundWorker.IsBusy)
                    AddHandler m_BackgroundWorker.DoWork, Sub(s, e) m_StartCommand.OnCanExecuteChanged(s, e)
                    AddHandler m_BackgroundWorker.RunWorkerCompleted, Sub(s, e) m_StartCommand.OnCanExecuteChanged(s, e)
                End If
                Return m_StartCommand
            End Get
        End Property
        Private m_StartCommand As ActionCommand
        <Description("Close Application"), DisplayName("Close")>
        Public ReadOnly Property CloseCommand As ICommand
            Get
                If m_CloseCommand Is Nothing Then
                    m_CloseCommand = New ActionCommand(Sub() Close(), Function() Not m_BackgroundWorker.IsBusy)
                    AddHandler m_BackgroundWorker.DoWork, Sub(s, e) m_StartCommand.OnCanExecuteChanged(s, e)
                    AddHandler m_BackgroundWorker.RunWorkerCompleted, Sub(s, e) m_StartCommand.OnCanExecuteChanged(s, e)
                End If
                Return m_CloseCommand
            End Get
        End Property
        Private m_CloseCommand As ActionCommand

        Private Shared Sub Close()
            Application.Current.Shutdown()
        End Sub

        <Description("Status Message"), DisplayName("Status Message")>
        Public Property StatusMessage As String
            Get
                Return m_StatusMessage
            End Get
            Set(ByVal value As String)
                If (m_StatusMessage <> value) Then
                    m_StatusMessage = value
                    OnPropertyChanged(Me, New PropertyChangedEventArgs("StatusMessage"))
                End If
            End Set
        End Property
        Private m_StatusMessage As String

        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If m_BackgroundWorker IsNot Nothing Then
                    m_BackgroundWorker.Dispose()
                    m_BackgroundWorker = Nothing
                End If
            End If
        End Sub
        Protected Overrides Sub Finalize()
            Dispose(False)
        End Sub


    End Class
End Namespace

